<?php

declare(strict_types=1);

namespace app\controller\admin;

use app\model\admin\CateModel;
use app\model\admin\Pact;
use app\model\admin\Poster;
use think\facade\Validate;
use think\Request;

class PosterController extends AdminBaseController
{
    // 获取协议
    public function getList(Request $request)
    {
        $params = $request->param(['name', 'status', 'type', 'created_at']);
        // $list = Poster::scope('where', $params)->order('id', 'desc')->paginate([
        //     'list_rows' => $this->limit,
        //     'page' => $this->page,
        //     'var_page' => $this->pageVar
        // ]);
        $list = Poster::scope('where', $params)->order('id', 'desc')->select();
        return success($list);
    }
    // 设置
    public function del($id, Request $request)
    {
        Poster::destroy($id);
        return success();
    }
    public function detail($id, Request $request)
    {
        $res = Poster::find($id);
        return success($res);
    }
    public function add(Request $request)
    {
        $payload = $request->param();
        $validate = Validate::rule([
            "name" => "require",
            "bg_img" => "require",
            "status" => "require",
            "type" => "require"
        ]);
        $res = $validate->check($payload);
        if (!$res) {
            return error($validate->getError());
        }
        Poster::create($payload);
        return success();
    }
    public function edit($id, Request $request)
    {
        $params = $request->param();
        Poster::update(
            [
                "name" => $params['name'],
                "type" => $params['type'],
                "status" => $params['status'],
                "bg_img" => $params['bg_img'],
                "avatar_info" => $params['avatar_info'],
                "nickname_info" => $params['nickname_info'],
                "qrcode_info" => $params['qrcode_info'],
            ],
            [
                "id" => $id
            ]
        );
        return success();
    }
}
